misra.py: R14.2: Verify for loop counter modification#2409
Conversation
Add additional check to detect modification of loop counter in loop body. Related issue: https://trac.cppcheck.net/ticket/9490 Add small fix that treat all assignment operators defined in N1750 6.5.16 as has side affects. This will affects rules 13.1, 13.3, 13.5 and allow to catch some false negatives.
|
I have merged the PR with the 14.2 tests. Can you merge Head into this PR please. |
|
@danmar does Cppcheck supports multiple variables declaration in N1750 allows this declaration at 6.8.5.3:
|
Yes of course we should support that. Something is missing in cppcheck for that. Strange that we have not noticed before! Awesome find! 👍 |
|
I created the ticket https://trac.cppcheck.net/ticket/9516 |
|
I am still not sure about the Section "8.14" in the misra pdf contains a definition for a loop counter variable;
So for instance, there is no loop counter variable here: |
|
@danmar that's right, this PR adds an additional check for variables that defined in the first clause of We can add more checks for loop counters from outer scopes and loop control flags defined in MISRA document as well. But it will be a bit more complicated due to lack of concrete examples and some unclear definitions in MISRA. I suppose it should be implemented incrementally in other PRs. |
|
if I have to choose Do you believe that there will not be false positives caused by this PR? To avoid false positives it seems to me that the |
|
I think I got what you mean. Are you about the case when we declare variable in first clause of for loop and it's value doesn't involved in decision to exit loop? This variable doesn't satisfy loop counter definition for MISRA document and it's modification is allowed. Something like this: int y = 0; // loop counter
for (int i = 0; y < 10; y++) {
i++; // 14.2. false positive?
}Am I right? But in this case we should generate violation of rule 14.2 in
So we want: int y = 0;
for (int i = 0; y < 10; y++) { // 14.2. violation in first clause
i++; // no warning
}And in the following case: int y = 0;
for (int i = 0; y < 10; y++) {
if (++i > 5) { // 14.2
break;
}
}
Okay, I'll check it a little later. |
|
yes I agree with that. |
|
@danmar I've corrected it according to your review. |
Add additional check to detect modification of loop counter in loop body. Related issue: https://trac.cppcheck.net/ticket/9490
Add small fix that treat all assignment operators defined in N1750 6.5.16 as has side affects. This will affects rules 13.1, 13.3, 13.5 and allow to catch some false negatives.